#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import asyncio
import sys
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root / 'src'))

from database.chromadb_client import ChromaDBClient
from config import load_config

async def check_chromadb_conversations():
    """检查ChromaDB中的对话数据结构"""
    print("=== 检查ChromaDB对话数据结构 ===")
    
    # 加载配置
    config = load_config()
    
    # 连接ChromaDB
    client = ChromaDBClient(config)
    await client.connect()
    
    try:
        # 获取conversations集合
        collection = client.get_collection('conversations')
        
        # 获取集合统计
        stats = await client.get_collection_stats('conversations')
        print(f"\nChromaDB conversations集合统计: {stats}")
        
        # 获取一些示例数据
        result = collection.get(limit=5, include=['metadatas', 'documents'])
        
        print(f"\n获取到 {len(result['ids'])} 个示例文档:")
        for i, (doc_id, metadata, document) in enumerate(zip(result['ids'], result['metadatas'], result['documents'])):
            print(f"\n示例 {i+1}:")
            print(f"  ID: {doc_id}")
            print(f"  Metadata: {metadata}")
            print(f"  Document: {document[:100]}..." if len(document) > 100 else f"  Document: {document}")
        
        # 测试搜索功能
        print("\n=== 测试搜索功能 ===")
        search_results = await client.search_conversations(
            query="测试",
            user_id="test_user",
            limit=3
        )
        
        print(f"\n搜索'测试'的结果数量: {len(search_results)}")
        for i, result in enumerate(search_results):
            print(f"\n搜索结果 {i+1}:")
            for key, value in result.items():
                if isinstance(value, str) and len(value) > 100:
                    print(f"  {key}: {value[:100]}...")
                else:
                    print(f"  {key}: {value}")
            
    finally:
        await client.close()

if __name__ == "__main__":
    asyncio.run(check_chromadb_conversations())